from django.contrib import admin
from .models import VpnPort
from .openvpn import OpenvpnPortUpdate
import logging
# Register your models here.


@admin.register(VpnPort)
class VpnPortAdmin(admin.ModelAdmin):
    list_display = ('port',)
    def save_model(self, request, obj, form, change):
        if change is False:
            objn = self.model.objects.filter()
            if len(objn) > 0:
                return
        obj.save()
        OpenvpnPortUpdate.update(obj.port)
        
    def delete_model(self, request, obj):
        objn = self.model.objects.filter()
        # logging.debug('enter delete_model')
        if len(objn) <= 1:
            # logging.debug('no need delete')
            return
        obj.delete()
